package com.ivanceras.client.view;

import java.util.ArrayList;
import java.util.List;

import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.HTML;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.VerticalPanel;
import com.google.gwt.user.client.ui.Widget;
import com.ivanceras.client.SimulationService;
import com.ivanceras.client.SimulationServiceAsync;
import com.ivanceras.client.Question;
import com.ivanceras.client.Questionnaire;


/**
 * Display the questions of a questionnaire
 * @author jovansonleec
 *
 */
public class QuestionnaireViewer {
	public Questionnaire questionnaire;
	public ArrayList<QuestionViewer> questionViewers = new ArrayList<QuestionViewer>();
	private VerticalPanel questionsPanel = new VerticalPanel(); //holds the questions
	public VerticalPanel questionnairePanel = new VerticalPanel(); //holds questinsPanel + question Creator
	HTML totalItems = new HTML();
	public QuestionEdit questionCreator;
	public HTML lastItem = new HTML();
	
	public QuestionnaireViewer(Questionnaire questionnaire){
		this.questionnaire = questionnaire;
	}
	public Widget getWidget(){
		getQuestions(this.questionnaire.questionnaireID);
		HTML vspacer = new HTML();
		vspacer.setHeight("20px");
		questionnairePanel.add(vspacer);
		questionCreator = new QuestionEdit(questionnaire.questionnaireID);
		questionCreator.questionnaireViewer = this;
		Widget qew = questionCreator.getWidget(); 
		//qew.setVisible(false);
		HorizontalPanel qnePanel = new HorizontalPanel();
		qnePanel.add(lastItem);
		HTML spacer = new HTML();
		spacer.setWidth("25px");
		qnePanel.add(spacer);
		qnePanel.add(qew);
		questionnairePanel.add(getQuestionnaireHeader());
		questionnairePanel.add(questionsPanel);
		questionnairePanel.add(getAnswerCheckerUI());
		HTML qnrSeparator = new HTML();
		qnrSeparator.setHeight("30px");
		qnrSeparator.setStyleName("qnr-qne-separator");
		questionnairePanel.add(qnrSeparator);
		questionnairePanel.add(qnePanel);
		//questionnairePanel.setBorderWidth(1);
		return questionnairePanel;
	}
	public Widget getAnswerCheckerUI(){
		HorizontalPanel checkPanel = new HorizontalPanel();
		Button checkButton = new Button("Check my answers...");
		checkButton.addClickHandler(new ClickHandler(){

			@Override
			public void onClick(ClickEvent event) {
				//Window.alert("Are you sure you wanted to check your answer");
				markUserAnswers();
			}
			
		});
		checkPanel.add(checkButton);
		return checkPanel;
	}
	
	public void markUserAnswers(){
		for(int i = 0; i < questionViewers.size(); i++){
			QuestionViewer qnv = questionViewers.get(i);
			qnv.setIndicator();
		}
	}
	public void setTotalItems(){
		totalItems.setHTML("("+questionnaire.items+" items)");
	}

	public Widget getQuestionnaireHeader(){
		VerticalPanel headerPanel = new VerticalPanel();
		headerPanel.add(new HTML("<b>"+this.questionnaire.title+"</b>"));
		headerPanel.add(totalItems);
		HorizontalPanel hpanel = new HorizontalPanel();
		HTML hspacer = new HTML();
		hspacer.setWidth("20px");
		hpanel.add(hspacer);
		HTML description = new HTML(this.questionnaire.description);
		hpanel.add(description);
		hpanel.setCellWidth(hspacer, "1%");
		hpanel.setCellWidth(description, "99%");
		hpanel.setWidth("100%");
		headerPanel.add(hpanel);
		HTML headerSpacer = new HTML();
		headerSpacer.setHeight("20px");
		headerPanel.add(headerSpacer);
		return headerPanel;
	}
	public void displayQuestion(){
		lastItem.setHTML("<b>"+(questionnaire.questions.size()+1)+".</b>");
		setTotalItems();
		int size = questionnaire.questions.size();
		for(int i = 0; i < size; i++){
			Question question = questionnaire.questions.get(i);
			question.itemNo = new Long(i+1);
			addQuestion(question);
		}
	}
	public void addQuestion(Question question){
		questionnaire.questions.add(question);
		QuestionViewer qnViewer = new QuestionViewer(question); //dependency injection so we select the viewer then we inject the question, the former is we let the question choice its viewer, but we will have no way of accessing the viewer since the viewer is not serializable as it include code which intended only for the client side ie widgets.
		qnViewer.setItemNumber(question.itemNo);
		questionViewers.add(qnViewer);
		questionsPanel.add(qnViewer.getWidget());
		HTML separator = new HTML(" ");
		separator.setHeight("25px");
		questionsPanel.add(separator);
	}
	/**
	 * This will be a separate request since we don't want to retrieve questions for each questionnaires
	 * @param questionnaireKey
	 * @return 
	 */
	public void getQuestions(final long questionnaireKey){
		final SimulationServiceAsync simulationService = GWT.create(SimulationService.class);
		simulationService.getQuestions(questionnaireKey,
			new AsyncCallback<List<Question>>() {
				public void onFailure(Throwable caught) {
					Window.alert("Error"+caught.toString());
				}
				public void onSuccess(List<Question> result) {
						QuestionnaireViewer.this.questionnaire.questions = result; //assign the results to this questionnaire object 
						displayQuestion();
				}
			});
	}

}
